package app.actions;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JOptionPane;
import javax.swing.JTree;
import libs.Attendance;
import libs.AttendanceException;
import libs.Subject;

/**
 *          This action tries to remove subject from model.
 * 
 * @author  papi
 */
public class RemoveSubjectAction extends AbstractAction {

    private Attendance attendance;

    private JTree tree;

    public RemoveSubjectAction(Attendance attendance) {
        if (attendance == null) {
            throw new NullPointerException("attendance");
        }
        this.attendance = attendance;
    }

    public void setTree(JTree tree) {
        if (tree == null) {
            throw new NullPointerException("tree");
        }
        this.tree = tree;
    }

    @Override
    public void actionPerformed(ActionEvent evt) {
        Subject subject = (Subject)tree.getSelectionPath().getPathComponent(1);
        try {
            String title = java.util.ResourceBundle.getBundle("app/frames/language").getString("QuestionMSG");
            String message = java.util.ResourceBundle.getBundle("app/frames/language").getString("ReallyRemoveSubjectMSG")
                    + " " + subject;
            if(JOptionPane.showConfirmDialog(null,message,title,
                JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE) == 0)
            {
                attendance.removeSubject(subject);
                tree.getModel().valueForPathChanged(tree.getSelectionPath().getParentPath(), null);
            }
        } catch(AttendanceException ex) {
            JOptionPane.showConfirmDialog(null,
                    java.util.ResourceBundle.getBundle("app/frames/language").getString("SubjectWithSeminarsMSG"),
                java.util.ResourceBundle.getBundle("app/frames/language").getString("Error"),
                JOptionPane.DEFAULT_OPTION, JOptionPane.ERROR_MESSAGE
            );
        }
    }


}
